package com.janlei.acc.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者
 */
public abstract class Subject {

    private final List<Observer> observers = new ArrayList<>();

    void addObserver(Observer observer) {
        observers.add(observer);
    }

    void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    void advice() {
        observers.forEach(Observer::response);
    }
}

/**
 * 观察者
 */
interface Observer {
    void response();
}


class Boss extends Subject {
    public void closing() {
        System.out.println("box is closing!!!");
        advice();
    }
}

class Worker implements Observer {

    @Override
    public void response() {
        System.out.println("Working!!!");
    }
}